import { View, Text, Button } from "@tarojs/components";
import Taro, { useLoad } from "@tarojs/taro";
import "./index.scss";

export default function Index() {
  const intance = Taro.getCurrentInstance();

  // 将所有页面加载逻辑放在这里
  useLoad((options: any) => {
    // 方式一：拿到页面传递过来的url参数
    console.log("Detail01=", options);

    // 只兼容微信小程序
    if (process.env.TARO_ENV === "weapp") {
      const eventChannel = intance.page?.getOpenerEventChannel?.(); // 获取事件通道
      eventChannel?.on("acceptDataFromHomePage", (data: any) => {
        console.log("Detail01 eventChannel=", data);
      });
    }

    // 方式二: 拿到页面传递过来的url参数
    console.log("页面传递过来的url参数Detail01=>", intance.router?.params);
  });

  function goToDetail07() {
    Taro.navigateTo({
      url: "/pages/detail04/index",
    });
  }

  function goToDetail08() {
    Taro.navigateTo({
      url: "/pages/detail05/index?name=liujun",
    });
  }

  return (
    <View className="index">
      <Text>页面的生命周期</Text>
      <Button onClick={goToDetail07}>goToDetail04 class navigate</Button>
      <Button onClick={goToDetail08}>goToDetail05 hooks navigate</Button>
    </View>
  );
}
